const api = require("./api.js");
exports.getCats = () => {
    return new Promise((resolve, reject) => {
        let storage = localStorage.getItem("cats");
        if(storage){
        	storage = JSON.parse(storage);
        }else{
        	storage = {};
        };
		let now = new Date().getTime();
		if(storage.expired && now < storage.expired){

			console.log("------从缓存中获取cats-------");
			resolve(storage.data);
		}else{
			console.log("------缓存中不存在cats-------");
			return api.process_catList().then(res=>{
				let cats = res.result;
				//存在缓存中
				let expired = new Date().getTime() + 12 * 60 * 60 * 1000;
				
				let storage_result = {data: res.result, expired};

				localStorage.setItem("cats", JSON.stringify(storage_result));
				resolve(cats);
			});
		};
    });
};